﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
using System.IO.IsolatedStorage;
using System.IO;
using System.Net.NetworkInformation;
using BiHCurrencyConverter.Model;
using BiHCurrencyConverter.Resources;
using System.Globalization;

namespace BiHCurrencyConverter
{
    public partial class Settings : PhoneApplicationPage
    {
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        IsolatedStorageFile fileStore = IsolatedStorageFile.GetUserStoreForApplication();
        
        public Settings()
        {
            InitializeComponent();
        }

        private void btnRefreshData_Click(object sender, RoutedEventArgs e)
        {
            if (NetworkInterface.GetIsNetworkAvailable())
            {
                try
                {
                    WebClient webClient = new WebClient();
                webClient.DownloadStringAsync(new Uri("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml"));
                webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            }
                catch (Exception)
                {
                    
                    MessageBox.Show(AppResources.msgUnableConn);
                }
            }
                
            else
            {
                MessageBox.Show(AppResources.msgDisconnected);
            }
            
            
        }

        void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            XDocument xmlResult = XDocument.Parse(e.Result);
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream("eurofxref-daily.xml", FileMode.Truncate, FileAccess.ReadWrite, fileStore);
            try
            {
                StreamWriter writeFile = new StreamWriter(stream);
                xmlResult.Save(writeFile);
                writeFile.Close();
            }
            catch (Exception)
            {
                MessageBox.Show(AppResources.msgCustomErr);
            }

            Utility.InitializeOrUpdateSettings();
            // REF01-MVVM
            //MainPage.InitializeCurrencyCollection();

            App.ViewModel.InitializeCurrencyCollection();


            MessageBox.Show(AppResources.msgSuccessRefresh);
            btnRefreshData.IsEnabled = false;
        }

        private void btnAddCurrency_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (tbSymbol.Text != String.Empty && tbRate.Text != String.Empty)
                {
                    settings[tbSymbol.Text] = Convert.ToDouble(tbRate.Text);
                    // REF01-MVVM
                    //if (MainPage.Currencies != null)
                    //{
                    //    MainPage.Currencies.Insert(2, new Currency
                    //    {
                    //        Symbol = tbSymbol.Text,
                    //        Rate = Convert.ToDouble(tbRate.Text),
                    //        ImageUrl = "/images/flags/" + tbSymbol.Text + ".png"
                    //    });
                    //}

                    if (App.ViewModel.Currencies != null)
                    {
                        App.ViewModel.Currencies.Insert(2, new Currency
                        {
                            Symbol = tbSymbol.Text,
                            Rate = Convert.ToDouble(tbRate.Text.Replace(".", CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator)),
                            ImageUrl = "/images/flags/" + tbSymbol.Text + ".png"
                        });
                    }
                    MessageBox.Show(String.Format(AppResources.msgSuccessAdd, tbSymbol.Text, tbRate.Text));
                }
                else
                {
                    MessageBox.Show(AppResources.msgMissingValue);
                }                
                
            }
            catch (Exception)
            {

                MessageBox.Show(String.Format(AppResources.msgUnableAdd, tbSymbol.Text));
            }
            
            
        }
    }
}